package com.ukefu.webim.util.impl;

import java.io.IOException;
import java.util.List;

import org.elasticsearch.common.lang3.StringUtils;
import org.lionsoul.jcseg.tokenizer.core.JcsegException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ukefu.core.UKDataContext;
import com.ukefu.util.ai.AiUtils;
import com.ukefu.webim.service.repository.SceneItemRepository;
import com.ukefu.webim.util.event.AiMessageSmartProcesser;
import com.ukefu.webim.util.server.message.ChatMessage;
import com.ukefu.webim.web.model.SceneItem;

@Component
public class AiMessageSmartProcesserImpl implements AiMessageSmartProcesser{

	@Autowired
	private SceneItemRepository sceneItemRes ;
	
	@Override
	public ChatMessage process(ChatMessage message) {
		ChatMessage retChatMessage = null ;
		try {
			String id = AiUtils.getAiDicTrie().search(message.getMessage()) ;
			if(!StringUtils.isBlank(id)){
				List<SceneItem> sceneItemList = sceneItemRes.findByOrgiAndSceneidAndItemtype(message.getOrgi() , id , UKDataContext.AiItemType.AIREPLY.toString()) ;
				if(sceneItemList!=null &&  sceneItemList.size() >0){
					SceneItem sceneItem = sceneItemList.get(0) ;
					retChatMessage = new ChatMessage();
					retChatMessage.setMessage(sceneItem.getContent());
					retChatMessage.setAppid(message.getAppid());
					retChatMessage.setUserid(message.getUserid());
					retChatMessage.setUsername("小E");
					retChatMessage.setChannel(message.getChannel());
					retChatMessage.setType(message.getType());
					retChatMessage.setContextid(message.getContextid());
					retChatMessage.setOrgi(message.getOrgi());
					
					/**
					 * 命中结果，记录日志，记录DEBUG信息，下个版本增加功能
					 */
				}
			}
		} catch (IOException | JcsegException e) {
			e.printStackTrace();
		}
		return retChatMessage;
	}

}
